﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cosmetic.DataEntity;

namespace Cosmetic.Logic
{
	/// <summary>
	/// activity manager class
	/// </summary>
	public class ActivityManager : LogicBase
    {
		public enum EState
		{
			All,
			Progress,
			Close
		}


		/// <summary>
		/// Insert a new activity into database
		/// </summary>
		/// <param name="anActivity">new activity</param>
		public void Insert(Activity anActivity)
		{
			m_CosmeticDC.Activities.InsertOnSubmit(anActivity);
			m_CosmeticDC.SubmitChanges();
		}

		/// <summary>
		/// Delete Activity
		/// </summary>
		/// <param name="anActivity">The Activity want delete</param>
		public void Delete(Activity anActivity)
		{
			m_CosmeticDC.Activities.DeleteOnSubmit(anActivity);
			m_CosmeticDC.SubmitChanges();
		}



		/// <summary>
		/// get Activity by Activity id
		/// </summary>
		/// <param name="aActivityId">the specify Activity id</param>
		/// <returns></returns>
		public Activity GetById(Guid aActivityId)
		{
			List<Activity> theActivityList	= (from theActivity in m_CosmeticDC.Activities
												   where theActivity.ActivityId == aActivityId
												   select theActivity).ToList();

			if (theActivityList.Count == 0)
			{
				return null;
			}
			return theActivityList[0];
		}

		/// <summary>
		/// get activities by state.
		/// </summary>
		/// <param name="aState">activity state</param>
		/// <returns>activity list</returns>
		public List<Activity> GetByState(EState aState)
		{
			switch (aState)
			{
				case EState.Progress:
					return (from theActivity in m_CosmeticDC.Activities
							where DateTime.Today.Date < theActivity.EndDate.Date
							orderby theActivity.StartDate descending
							select theActivity).ToList();
					break;
				case EState.Close:
					return (from theActivity in m_CosmeticDC.Activities
                            where DateTime.Today.Date > theActivity.EndDate.Date
							orderby theActivity.StartDate descending
							select theActivity).ToList();
					break;
				case EState.All:
				default:
					return (from theActivity in m_CosmeticDC.Activities
							orderby theActivity.StartDate descending
							select theActivity).ToList();
					break;
			}
		}

        /// <summary>
        /// get activities by state.
        /// </summary>
        /// <param name="aState">activity state</param>
        /// <returns>activity list</returns>
        public List<Activity> GetByState(EState aState, string CategoryName)
        {
            switch (aState)
            {
                case EState.Progress:
                    return (from theActivity in m_CosmeticDC.Activities
                            where DateTime.Today.Date < theActivity.EndDate.Date && theActivity.CategoryName == CategoryName && theActivity.Other ==""
                            orderby theActivity.StartDate descending
                            select theActivity).ToList();
                    break;
                case EState.Close:
                    return (from theActivity in m_CosmeticDC.Activities
                            where DateTime.Today.Date > theActivity.EndDate.Date && theActivity.CategoryName == CategoryName && theActivity.Other == ""
                            orderby theActivity.StartDate descending
                            select theActivity).ToList();
                    break;
                case EState.All:
                default:
                    return (from theActivity in m_CosmeticDC.Activities
                            where theActivity.CategoryName == CategoryName && theActivity.Other == ""
                            orderby theActivity.StartDate descending
                            select theActivity).ToList();
                    break;
            }
        }


        /// <summary>
        /// get activities by state.
        /// </summary>
        /// <param name="aState">activity state</param>
        /// <returns>activity list</returns>
        public List<Activity> GetByState_CategoryName(EState aState, string CategoryName)
        {
            switch (aState)
            {
                case EState.Progress:
                    return (from theActivity in m_CosmeticDC.Activities
                            where DateTime.Today.Date < theActivity.EndDate.Date && theActivity.CategoryName == CategoryName && theActivity.Other == ""
                            orderby theActivity.StartDate descending
                            select theActivity).ToList();
                    break;
                case EState.Close:
                    return (from theActivity in m_CosmeticDC.Activities
                            where  theActivity.CategoryName == CategoryName && theActivity.Other != ""
                            orderby theActivity.StartDate descending
                            select theActivity).ToList();
                    break;
                case EState.All:
                default:
                    return (from theActivity in m_CosmeticDC.Activities
                            where theActivity.CategoryName == CategoryName && theActivity.Other == ""
                            orderby theActivity.StartDate descending
                            select theActivity).ToList();
                    break;
            }
        }

        /// <summary>
        /// get AD by Category name and page name.
        /// </summary>
        /// <param name="aState">activity state</param>
        /// <returns>activity list</returns>
        public List<Activity> GetAd(string CategoryName, string pageName)
        {
            //这里other字段代表这个广告是哪个页面的哪个位置。
            var aa = (from act in m_CosmeticDC.Activities
                      where act.CategoryName == CategoryName && act.Other == pageName
                      select act).ToList();
            return aa;
        }

        public List<Activity> GetAd(string pageName)
        {
            //这里other字段代表这个广告是哪个页面的哪个位置。
            var aa = (from act in m_CosmeticDC.Activities
                      where act.Other == pageName
                      select act).ToList();
            return aa;
        }

	}
}
